import 'package:family_expense/features/bank_card/widgets/bank_shared.dart';
import 'package:family_expense/features/income_expense/widgets/income_expense_detail_shared.dart';
import 'package:family_expense/features/journey/models/journey_income_expense_detail.dart';
import 'package:flutter/material.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

/// Builds the journey income/expense detail list tile.
/// [keywords] for display in highlight.
class JourneyIncomeExpenseDetailListTile extends StatelessWidget {
  const JourneyIncomeExpenseDetailListTile(
    this.item,
    this.onTap, {
    super.key,
    this.region,
    this.keywords,
  });

  final JourneyIncomeExpenseDetail item;
  final GestureTapCallback onTap;
  final String? region;
  final List<String>? keywords;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: keywords?.isNotEmpty == true
          ? RichText(
              text: TextSpan(
                style: DefaultTextStyle.of(context).style,
                children: buildTextSpansWithKeywords(item.details!, keywords),
              ),
            )
          : Text(item.details!, style: const TextStyle(fontSize: 14.0)),
      subtitle: Text(
        '${region ?? ''}${item.location!} @ ${item.incomeExpenseDate!.convertDateToFriendlyText(DatePatterns.longDate)}',
        style: const TextStyle(fontSize: 12.0, color: Colors.black54),
      ),
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (item.bankCard?.id.isNotNullOrEmpty == true)
            BankLogo(item.bankCard!.bank!, width: 14.0),
          if (item.bankCard?.id.isNotNullOrEmpty == true)
            const SizedBox(width: 5.0),
          Align(
            alignment: Alignment.topCenter,
            child: AmountSymbol(
              item.amount!,
              size: 10.0,
              marginTop: 20.0,
              color: item.amount! > 0
                  ? Colors.black54
                  : IncomeExpenseDetailShared.expenseAmountColor,
            ),
          ),
          Text(
            formatNumber(item.amount!.abs()),
            style: TextStyle(
              fontSize: 16.0,
              color: item.amount! > 0
                  ? Colors.black54
                  : IncomeExpenseDetailShared.expenseAmountColor,
            ),
          ),
          const SizedBox(width: 2.0),
          Text(item.currency!, style: Theme.of(context).textTheme.bodySmall),
        ],
      ),
      onTap: onTap,
    );
  }
}
